﻿package b3.widget.mail
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	import b3.widget.warehouse.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class MailTypePane extends TabController
	{
		public function MailTypePane()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("WarehouseBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("TabTitleBgr1");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("TabTitleBgr2");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage3");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp3");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown3");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage3");
			super(TabControllerBackground, CloseButton);
			this.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			var tab:Tab = new Tab("许可证", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("RMB\n 道具", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("非RMB\n  道具", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			this.setTabTitlePos(52, -25); 
			this.setTabPagePos(84, 75);
			this.setCloseButtonPos(235, 353);
			//加载所有物品
			loadMailItemList();
			
			this.x = -this.width / 2;
			this.y = -this.height / 2;
		}
		
		private function loadMailItemList():void
		{
			//排序物品
			var objectIndexes:Array = new Array();
			var objectConfig:ObjConfig;
			for (var indexStr:String in Config.objConfigs)
			{
				objectConfig = Config.objConfigs[indexStr];
				if (objectConfig.storeTabIndex >= 0)
				{
					objectIndexes.push(objectConfig.index);
				}
			}
		    objectIndexes.sort(sortObjects);
		    //加载物品
			for (var j:int = 0; j < objectIndexes.length; j++)
			{
				var index:int = objectIndexes[j];
				objectConfig = Config.objConfigs[index];
				var mailTypeItem:MailTypeItem = new MailTypeItem(index);
				mailTypeItem.addEventListener(MouseEvent.CLICK, onMailTypeItemClick);
				var tabIndex:int = objectConfig.storeTabIndex - 1 < tabs.length ? objectConfig.storeTabIndex - 1 : tabs.length - 1; 
				tabs[tabIndex].addItem(mailTypeItem, 92, 112);
			}
		}
		
		private function onMailTypeItemClick(event:Event):void
		{
			var mailTypeItem:MailTypeItem = event.currentTarget as MailTypeItem;
			var selectDialog:ObjNumSelectDialog = new ObjNumSelectDialog(mailTypeItem.objectIndex);
			WidgetManager.getInstance().popOut([selectDialog], true);
			close();
		}
		
		//排序
		private function sortObjects(a:int, b:int):int
		{
			var objectConfigA:ObjConfig = Config.objConfigs[a];
			var objectConfigB:ObjConfig = Config.objConfigs[b];
			if (objectConfigA.index < objectConfigB.index)
			{
				return -1;
			}
			else if (objectConfigA.index > objectConfigB.index)
			{
				return 1;
			}
			return 0;
		}
	}
}